package com.bluemobi.system.util;

import java.io.File;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.util.AlternativeJdkIdGenerator;

/**
 * 功能说明: 系统工具类
 * 
 * @author: wangchen
 */
public class SwUtil {
	private static AlternativeJdkIdGenerator aj = null;

	static {
		if (aj == null) {
			aj = new AlternativeJdkIdGenerator();
		}
	}

	public static void main(String[] args) {
		System.out.println(generateOrderNo());

	}

	/**
	 * 功能说明: 获取UUID
	 * 
	 * @Example SysUtil.getUUID
	 * @author wangchen
	 */
	public static String getUUID() {
		return aj.generateId().toString().replace("-", "").toLowerCase().toString();
	}

	/**
	 * 功能说明: APP登录密码加密
	 * 
	 * @Example SysUtil.encodePassword
	 * @author wangchen
	 */
	public static String encodeAppPassword(String password) {
		return new Md5PasswordEncoder().encodePassword(password, "");
	}

	/**
	 * 功能说明: 获取IP 非127.0.0.1
	 */
	public static String getHostIp() {
		try {
			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements()) {
					InetAddress ip = (InetAddress) addresses.nextElement();
					// loopback地址即本机地址，IPv4的loopback范围是127.0.0.0~127.255.255.255
					if (ip != null && ip instanceof Inet4Address && !ip.isLoopbackAddress()
							&& ip.getHostAddress().indexOf(":") == -1) {
						return ip.getHostAddress();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/* 获取IP和端口，过滤127.0.0.1 */
	public static String getIpPort(HttpServletRequest request) {
		String ip = SwUtil.getHostIp();
		int port = request.getLocalPort();
		return ("http:" + File.separator + ip + ":" + port + File.separator + request.getContextPath().replace("/", ""))
				.replaceAll("\\\\", "/");
	}

	/**
	 * 获取6-10 的随机位数数字
	 * 
	 * @param length
	 *            想要生成的长度
	 * @return result
	 */
	public static String getRandom620(Integer length) {
		String result = "";
		Random rand = new Random();
		int n = 20;
		if (null != length && length > 0) {
			n = length;
		}
		int randInt = 0;
		for (int i = 0; i < n; i++) {
			randInt = rand.nextInt(10);
			result += randInt;
		}
		return result;
	}

	/**
	 * 功能说明: 支付订单号获取
	 * 
	 * @Example SwUtil.generateOrderNo
	 * @return
	 * @author wangchen
	 * @DATE 2018年6月27日下午11:20:00
	 */
	public static String generateOrderNo() {
		// 格式化当前时间
		SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String strDate = sfDate.format(new Date());
		// 为了防止高并发重复,再获取3个随机数
		String random = getRandom620(3);
		// 最后得到20位订单编号。
		return strDate + random;
	}
}
